package com.red.wine.sql;
import java.util.ArrayList;
import java.util.List;

import com.red.wine.model.CityListInfo;
import com.red.wine.util.StringUtil;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class CitySqlHelper {

	private SQLiteDatabase db = null;

	/**
	 * 打开数据库
	 */
	public void open(SqlHelper dh) {
		// dh = new SqlHelper(null, "atsmart", null,SqlHelper.getDBVersion());
		db = dh.getWritableDatabase();
	}

	public synchronized void addClientInfo(CityListInfo at) {
		if (at == null) {
			return;
		}
		String strSql = "insert or replace into city_dataa_list(id,name,id1,pid,pinyin) values(?,?,?,?,?);";
		try {
			SQLiteStatement stm = db.compileStatement(strSql);
			stm.bindString(1, at.getId());
			stm.bindString(2, at.getName());
			stm.bindString(3, at.getId1());
			stm.bindString(4, at.getPid());
			stm.bindString(5, at.getPinyin());
			stm.execute();
			stm.clearBindings();
			stm.close();
		} catch (Exception e) {
			// will not happen
		}
	}

	public List<CityListInfo> getClientInfo(int begin, int pagesize,String keyword) {
		String sql ="";
		if(keyword==null||"".equals(keyword)){
			 sql = " select _id,id,name,id1,pid,pinyin from city_dataa_list limit "
						+ begin + "," + pagesize;
		}else{
//			 sql = " select _id,id,name,id1,pid,pinyin from city_dataa_list where pinyin like '%"+keyword+"%'  limit "
//						+ begin + "," + pagesize;
			sql = " select _id,id,name,id1,pid,pinyin from city_dataa_list where pinyin like '%"+keyword+"%'";
		}
		Cursor cursor = db.rawQuery(sql, null);
		CityListInfo historie = null;
		List<CityListInfo> nums = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				nums = new ArrayList<CityListInfo>();
				 
				for (int i = 0; i < cursor.getCount(); i++) {
					historie = new CityListInfo();
					historie.setId(cursor.getString(1));
					historie.setName(cursor.getString(2));
					historie.setId1(cursor.getString(3));
					historie.setPid(cursor.getString(4));
					historie.setPinyin(cursor.getString(5));
					if(!StringUtil.isEmptyOrNull(historie.getPinyin())){
						if(historie.getPinyin().substring(0, 1).equals(keyword)){
							Log.e("TAG", ";;;" + historie.getPinyin().substring(0, 1)+ ";;;" + historie.getPinyin());
							nums.add(historie);
						}
					}
					 
//					nums.add(historie);
					cursor.moveToNext();
				}
			}
		} catch (Exception e) {
			return null;
		} finally {
			cursor.close();
		}
		return nums;
	}

	 
	public int getCount() {
		String sql = " select _id from city_dataa_list ";
		Cursor cursor = db.rawQuery(sql, null);
		try {
			if (cursor != null && cursor.moveToFirst()) {
				return cursor.getCount();
			}
		} catch (Exception e) {
			return 0;
		} finally {
			cursor.close();
		}
		return 0;
	}

	public void deleteAttribute(int id) {
		// db.delete("position_attribute", null, null);
		db.execSQL("delete from city_dataa_list where pol_id="+id);
	}
	public void delete() {
		// db.delete("position_attribute", null, null);
		db.execSQL("delete from city_dataa_list ");
	}
}
